package org.usmile.algorithms.huawei.acm;

import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Scanner;

public class _打印机队列 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(sc.nextLine());
        String[][] tasks = new String[n][];

        for (int i = 0; i < n; i++) {
            tasks[i] = sc.nextLine().split(" ");
        }

        getResult(tasks);
    }

    public static void getResult(String[][] tasks) {
        HashMap<String, PriorityQueue<Integer[]>> printers = new HashMap<>();
        int fileId = 1;
        for (String[] task : tasks) {
            String type = task[0];
            String printerId = task[1];

            if ("IN".equals(type)) {
                String priority = task[2];
                Integer[] arr = {fileId, Integer.parseInt(priority)};
                printers.putIfAbsent(printerId, new PriorityQueue<>((a, b) -> b[1] - a[1]));
                printers.get(printerId).offer(arr);
                fileId++;
            } else {
                Integer[] arr = printers.get(printerId).poll();
                if (arr != null) {
                    System.out.println(arr[0]);
                } else {
                    System.out.println("NULL");
                }
            }
        }
    }
}
